1   package jrre.unittests;
2   
3   import junit.framework.*;
4   import jrre.*;
5   import jrre.instructionset.*;
6   import jrre.types.*;
7   import jrre.unittests.datastructures.*;
8   
9   import java.io.*;
10  
11  public class ObjectsTest extends TestCase {
12  
13     public void testArrayAverage(){
14  
15          System.out.println("\nS T A R T I N G   A R R A Y    A V E R A G E   T E S T:\n");
16  
17          JRRERunner runtime = new JRRERunner();
18          runtime.loadClass("target/test-classes/jrre/unittests/datastructures/ArrayManipulations");
19  
20          Type [] elementValues = new PrimitiveType[10];
21          
22          int value = 0;
23          int correctResult = 0;
24          for(int i=0;i < elementValues.length;i++){
25  
26              value = elementValues.length-i;
27              System.out.println("\tarray["+i+"] = "+value);
28  
29              elementValues[i] = new PrimitiveType(value);
30              correctResult += value;
31          }
32          correctResult = correctResult / elementValues.length;
33  
34  
35          ArrayInstance arrayParameter = new ArrayInstance(1, elementValues.length);
36          arrayParameter.setValues(elementValues);
37  
38          Type [] parameters = new Type[] { new ReferenceType(arrayParameter) };
39          PrimitiveType result = (PrimitiveType) runtime.callMethod("average([I)I", parameters);
40  
41          System.out.println("\nAverage value in array: "+result.getValue());
42          Assert.assertTrue("Testing that calling average(int [] data) == "+correctResult,
43                             result.getValue() == correctResult);
44     }
45  
46     public void NOtestArrayQuickSort(){
47  
48          System.out.println("\nS T A R T I N G    Q U I C K S O R T    T E S T:\n");
49  
50          JRRERunner runtime = new JRRERunner();
51          runtime.loadClass("target/test-classes/jrre/unittests/datastructures/ArrayManipulations");
52  
53          Type [] elementValues = new PrimitiveType[20];
54          
55          int value = 0;
56          int correctResult = 0;
57          java.util.Random randomGenerator = new java.util.Random();
58          for(int i=0;i < elementValues.length;i++){
59              
60              value = randomGenerator.nextInt(50);
61              elementValues[i] = new PrimitiveType(value);
62              correctResult += value;
63          }
64          correctResult = correctResult / elementValues.length;
65  
66          ArrayInstance arrayParameter = new ArrayInstance(1, elementValues.length);
67          arrayParameter.setValues(elementValues);
68  
69          Type [] parameters = new Type[] { new ReferenceType(arrayParameter) };
70          ReferenceType result = (ReferenceType) runtime.callMethod("sortArray([I)[I", parameters);
71  
72          ArrayInstance resultArray = (ArrayInstance)result.getValue();
73  
74          // Verify that the array is sorted.
75          boolean passed = true;
76          for(int i=0;i < resultArray.getSize()-1;i++){
77  
78              int thisValue = ((PrimitiveType)resultArray.getElement(i)).getValue();
79              int nextValue = ((PrimitiveType)resultArray.getElement(i+1)).getValue();
80  
81              if(thisValue > nextValue)
82                  passed = false;
83          }
84  
85          Assert.assertTrue("Testing that the array is sorted after quicksort algorithm.",
86                            passed);
87     }
88  
89     public void NOtestLinkedList(){
90  
91          System.out.println("\nS T A R T I N G    L I N K E D L I S T    T E S T:\n");
92  
93          JRRERunner runtime = new JRRERunner();
94  
95          // Load class containing factorial method.
96          runtime.loadClass("target/test-classes/jrre/unittests/datastructures/LinkedList");
97          
98          Type [] elementValues = { new PrimitiveType(2),
99                                    new PrimitiveType(9),
100                                   new PrimitiveType(3),
101                                   new PrimitiveType(25),
102                                   new PrimitiveType(11)};
103 
104         ArrayInstance arrayParameter = new ArrayInstance(1, elementValues.length);
105         arrayParameter.setValues(elementValues);
106 
107         Type [] parameters = new Type[] { new ReferenceType(arrayParameter) };
108         PrimitiveType result = (PrimitiveType) runtime.callMethod("fillList([I)V", parameters);
109 
110         /*
111         // Build test case input values 
112         int [] [] testInput = {{ 4, 2},
113                                { 2, 2},
114                                { 6, 3},
115                                { 400, 225},
116                                { 121, 11}};
117                                
118         int [] results = {2,
119                           2,
120                           3,
121                           25,
122                           11
123                          };
124         int resultsIndex = 0;
125             
126         for(int i=0;i < testInput.length;i++){
127 
128             int parameterOne = testInput[i][0];
129             int parameterTwo = testInput[i][1];
130             int correctResult = results[resultsIndex++];
131 
132             // Build parameters for method call.
133             Type [] parameters = new Type[] { new PrimitiveType(parameterOne), new PrimitiveType(parameterTwo) };
134 
135             // Call method and get the top of the operand stack
136             // after it returns (its return value).
137             PrimitiveType result = (PrimitiveType) runtime.callMethod("euclidsGCD(II)I", parameters);
138         
139             // Check that the result is correct.
140             Assert.assertTrue("Testing that euclidsGCD("+parameterOne+", "+parameterTwo+") == "+correctResult,
141                               result.getValue() == correctResult);
142 
143             System.out.println("euclidsGCD("+parameterOne+", "+parameterTwo+") returned: "+result.getValue());
144         }
145 
146         */
147     }
148 
149    public void setUp(){
150 
151    }
152 
153    public void tearDown(){
154 
155    }
156 
157 }
158 
This page was automatically generated by Maven